package com.jgy.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jgy.mapper.SpaceUserMapper;
import com.jgy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 姜光远
 * @date 2022/7/21 16:32
 */
public class AuthenticationEntryPointHandler implements AuthenticationEntryPoint {

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private SpaceUserMapper mapper;

    @Override
    public void commence(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse,
                         AuthenticationException e) throws IOException, ServletException {

        //List<SpaceUser> spaceUsers = mapper.selectList(null);
        //List<SpaceUser> collect = spaceUsers.stream().filter(data -> data.getId() != null).collect(Collectors.toList());
        //System.out.println(collect);
        httpServletResponse.setContentType("application/json; charset=utf-8");
        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(R.failed("未验证，请先登录!!").setCode(201)));
    }
}
